//
//  TSWithDrawCashViewController.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/5.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

protocol TSWithDrawBankViewControllerDelegate : NSObjectProtocol {
    func bankCompleteWithdraw()
}

class TSWithDrawBankViewController: TSBaseViewController {
    
    weak var delegate : TSWithDrawBankViewControllerDelegate? = nil
    /** 提现类型 0 -- 盈利钱包 1 -- 收益钱包 2 -- 中心钱包 */
    var withdrawType : Int = -1
    var model : TSCardListModel!
    
    var tableView : UITableView!
    lazy var sectionHeaderView : TSWithDrawCashSectionHeaderView = {
        let headerView = TSWithDrawCashSectionHeaderView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 158 + 17))
        return headerView
    }()
    lazy var sectionFooterView : TSWithDrawCashSectionFooterView = {
        var footerView = TSWithDrawCashSectionFooterView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 288))
        footerView.delegate = self
        return footerView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        configViews()
        setupNavigationTitle(title: "提现")
    }
}

//MARK: - TSWithDrawCashSectionFooterViewDelegate
extension TSWithDrawBankViewController : TSWithDrawCashSectionFooterViewDelegate {
    
    func fitToWithdraw(money: String, bankpwd: String) {
        withdrawRequest(money: money, bankpwd: bankpwd)
    }
}

//MARK: - UITableViewDelegate
extension TSWithDrawBankViewController : UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        sectionHeaderView.setupView(model: self.model)
        return sectionHeaderView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 158 + 17
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        
        sectionFooterView.setupView(withdrawType: withdrawType)
        return sectionFooterView
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 288
    }
}

//MARK: - UITableViewDataSource
extension TSWithDrawBankViewController : UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        var cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.self))
        
        if cell == nil {
            cell = UITableViewCell.init(style: .default, reuseIdentifier: NSStringFromClass(UITableViewCell.self))
        }
        
        return cell!
    }
}

//MARK: - Network Request
extension TSWithDrawBankViewController {
    
    func withdrawRequest(money:String, bankpwd:String) {
        
        var target : TMSNetworkTools!
        
        CBToast.showToastAction()
        if withdrawType == 0 {
            
            target = TMSNetworkTools.withdrawProfit(money: money, bankpwd: bankpwd)
            
        } else if withdrawType == 1 {
            
            target = TMSNetworkTools.withdrawProfit(money: money, bankpwd: bankpwd)
            
        } else {
            
            target = TMSNetworkTools.withdrawProfit(money: money, bankpwd: bankpwd)
        }
        
        TSNetworkManager.request(target, showHUD: true, success: { (data) in
            self.requestUserInfo()
        }) { (error) in
            CBToast.hiddenToastAction()
        }
    }
    
    func requestUserInfo() {
        
        TSNetworkManager.request(TMSNetworkTools.getUserInfo, showHUD: true, success: { (data) in
            
            CBToast.hiddenToastAction()
            
            let responseDic = data as? NSDictionary
            var model = TSLoginModel.deserialize(from: responseDic)!
            model.token = TSLoginManager.shared.token
            model.user_id = TSLoginManager.shared.user_id
            TSLoginManager.shared.setUserModel(model: model)
            
            CBToast.showToastAction(message: "提现成功")
            self.sectionFooterView.setupView(withdrawType: self.withdrawType)
            if self.delegate != nil {
                self.delegate?.bankCompleteWithdraw()
            }
            
        }) { (error) in
            CBToast.hiddenToastAction()
        }
    }
}

//MARK: - Initialize
extension TSWithDrawBankViewController {
    
    func configViews() {
        
        tableView = UITableView.init(frame: .zero, style: .plain)
        tableView.backgroundColor = WHITE_COLOR
        tableView.delegate = self;
        tableView.dataSource = self
        tableView.separatorStyle = .none
//        tableView.tableFooterView = tableFooterView
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(T_NAVBARHEIGHT)
        }

        if #available(iOS 11, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
    }
}
